package com.kins.executer.utils;

import com.alibaba.fastjson.JSONArray;
import com.kins.executer.constants.Constant;
import com.kins.executer.vo.MappingParamVo;
import io.micrometer.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;


/**
 * @Description:数据处理工具类
 * @Author: 申科
 * @Date:2025/11/12 10:31
 */

public class DataUtils {
    private static final Logger logger = LoggerFactory.getLogger(DataUtils.class);




    /**
     * 根据参数路径获取参数值
     */

    public static String getParamValue(Map map, String path, String spliter){
        if(StringUtils.isBlank(spliter)){
            spliter = Constant.POINT;
        }

        if(map == null){
            return null;
        }

        if(StringUtils.isEmpty(path)){
            return null;
        }
        String [] bomNames = StringUtils.split(path,spliter);
        Object parent = null;
        Map parentMap = null;
        parentMap = map;
        boolean isNull = false;
        for(int i = 0; i < bomNames.length-1; ++i){
            parent = parentMap.get(bomNames[i].trim());
            if(parent == null){
                isNull = true;
                break;
            }else{
                if(parent instanceof List){
                    List<Map<String, Object>> list = (List<Map<String, Object>>) parent;
                    if(list != null && !list.isEmpty()){
                        parentMap = list.get(0);
                    }
                } else if (parent instanceof JSONArray) {
                    JSONArray list = (JSONArray) parent;
                    if(list != null && !list.isEmpty()){
                        parentMap = list.getJSONObject(0);
                    }
                }else{
                    parentMap = (Map)parent;
                }
            }
        }
        if(isNull){
            return null;
        }
        return parentMap == null ? null : parentMap.get(bomNames[bomNames.length-1].trim());
    }


    /**
     * 根据拼接标识符凭借字符串
     */
    public static String getJoinStr(String split, String... strs){
        if(StringUtils.isBlank(split)){
            split = Constant.SPLIT_STR;
        }
        StringBuffer stringBuffer = new StringBuffer();
        int i = 0;
        int size = strs.length;
        for (String str : strs){
            if (i == size - 1){
                stringBuffer.append(str);
            }else {
                stringBuffer.append(str).append(split);
                i++;
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 服务编排ID服务编排版本
     */

    public static String getRectsKeyStr(MappingParamVo vo){
        String key = DataUtils.getJoinStrBySplit
                (
                        (vo.getApiName() == null ? "" : vo.getApiName()),
                        DataUtils.getJoinStr
                                (
                                        Constant.UNDERLINE,
                                        vo.getServiceArrangeId(),
                                        getVersionReplaceStr(vo.getServiceArrangeVersion(),true)
                                ),
                        vo.getNodeId(),
                        DataUtils.getJoinStr
                                (
                                        Constant.UNDERLINE,
                                        vo.getInterfaceId(),
                                        ""
                                )
                );
        return key;

    }

    public static Map<String,Object> supplyData(Map<String,Object> map, String info, String isRightInterface,Object startData){
        map.put(Constant.RUN_INFO_KEY,info);
        map.put(Constant.START_DATE_KEY,startData);
        map.put(Constant.END_DATE_KEY,System.currentTimeMillis());
        map.put(Constant.IS_RIGHTING_KEY,isRightInterface);
        return map;
    }
}
